/**
 * 
 */
package org.vsg.security.policy;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.security.model.domain.ResourceAssociation;
import org.vsg.security.model.domain.SysResource;
import org.vsg.security.vo.SecRole;

/**
 * @author Bill Vison
 *
 */
public class WebResourceAssoHandlerPolicy implements ResourceAssoHandlerPolicy {
	
	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(WebResourceAssoHandlerPolicy.class);
	

	/* (non-Javadoc)
	 * @see org.vsg.security.policy.ResourceAssoHandlerPolicy#handlePolicy(java.util.Set)
	 */
	@Override
	public Set<ResourceAssociation> handlePolicy(List<SecRole> roles)throws HandlerPolicyException {
		// TODO Auto-generated method stub
		
		Set<SysResource> existResource = new HashSet<SysResource>();

		Set<ResourceAssociation> grantAsso = new HashSet<ResourceAssociation>();
		
		
		SecRole secRole = null;
		Set<ResourceAssociation> roleResAssoSet = null;
		ResourceAssociation resourceAssoication = null;
		for (Iterator<SecRole> rolesIter = roles.iterator() ; rolesIter.hasNext();) {
			secRole = rolesIter.next();
			roleResAssoSet = secRole.getRole().getAssignedPermissions();
			
			for (Iterator<ResourceAssociation> raItera = roleResAssoSet.iterator() ; raItera.hasNext() ; ) {
				resourceAssoication = raItera.next();
				
				if (! existResource.contains(resourceAssoication.getResource()) ) {
					existResource.add( resourceAssoication.getResource() );
					grantAsso.add( resourceAssoication );
				}
				
			}
			
		}

		
		return grantAsso;
	}

}
